package com.sj.uabrowser.mvp.menu.view.adp;

import android.support.annotation.Nullable;
import android.text.TextUtils;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sj.uabrowser.R;
import com.sj.uabrowser.db.CollectHistory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class HistoryAdp extends BaseQuickAdapter<CollectHistory, BaseViewHolder> {

    private boolean isEdit = false;

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    public HistoryAdp(int layoutResId, @Nullable List<CollectHistory> data) {
        super(layoutResId, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, CollectHistory item) {
        String time = item.getTime();
        boolean isNullTime = TextUtils.isEmpty(time);
        if(!isNullTime)helper.setText(R.id.tv_header,getDateString(time));
        helper.setVisible(R.id.tv_header,!isNullTime);
//        int type = item.getType();
//        if(type== CollectHistory.TYPE_NET||type == CollectHistory.TYPE_KEYWORDS||type == CollectHistory.TYPE_NEWS){
//
//        }
        helper.setChecked(R.id.cb,item.isChecked());
        helper.setVisible(R.id.cb,isEdit);
        String title = item.getTitle();
        helper.setText(R.id.tv_title,title);
        String url = item.getUrl();
        helper.setText(R.id.tv_url,url);
        helper.setVisible(R.id.tv_title,!TextUtils.isEmpty(title));
        helper.setVisible(R.id.tv_url,!TextUtils.isEmpty(url));
    }

    private String getDateString(String time){
        if(TextUtils.isEmpty(time))return time;
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String today = sdf.format(new Date());
        String yesterday = sdf.format(new Date().getTime()-24*60*60*1000);
        if(TextUtils.equals(today,time)){
            return "今天";
        }else if(TextUtils.equals(yesterday,time)){
            return "昨天";
        }
       return time;
    }
}
